<template>
  <div id="RoutineDetails">
    <v-detail
      :detailData="inputData.detailData"
      :inputConfig="inputConfig"
      :rules="rules"
      @changeData="changeData"
      @pop="setData"
      ref="input"
    ></v-detail>
  </div>
</template>

<script>
import detail from "commons/Detail";
import baseMixin from "@/mixins/BaseMixin";

export default {
  name: "RoutineDetails",
  mixins: [baseMixin],
  props: {
    inputData: {
      type: Object
    }
  },
  data () {
    return {
      rules: {
        storerkey: [
          { required: true, validator: this.storerkey, trigger: 'blur' },
        ],
        sku: [
          { required: true, message: this.$inputPlease("config.sku"), trigger: 'blur' },
        ],
        descr: [
          { required: true, message: this.$inputPlease("common.desc"), trigger: 'blur' },
        ],
        packkey: [
          { required: true, message: this.$inputPlease("config.packaging"), trigger: 'blur' },
        ],
        class_: [
          { required: true, message: this.$selectPlease("common.class"), trigger: 'change' },
        ],
        stdcube: [
          { required: true, validator: this.verify, trigger: 'blur' },
        ],
        stdgrosswgt: [
          { required: true, validator: this.verify, trigger: 'blur' },
        ],
        stdnetwgt: [
          { required: true, validator: this.verify, trigger: 'blur' },
        ],
        tare: [
          { required: true, validator: this.verify, trigger: 'blur' },
        ],
        toexpiredays: [
          { validator: this.verify1, trigger: 'blur' },
        ],
        todeliverbydays: [
          { validator: this.verify1, trigger: 'blur' },
        ],
        tobestbydays: [
          { validator: this.verify1, trigger: 'blur' },
        ],
        shelflifeonreceiving: [
          { validator: this.verify1, trigger: 'blur' },
        ],
        shelflife: [
          { validator: this.verify1, trigger: 'blur' },
        ],
      },
      whseidflag: false,
      addflag: true,
      flag: true,
      ruleList:[],
      class_List:[]
    }
  },
  computed: {
    inputConfig () {
      return [
        [
          {
            type: "table",
            label: this.$t('config.shipper'),
            props: "storerkey",
            disabled: this.addflag ? this.addflag : this.whseidflag,
            tableConfig: [
              {
                label: this.$t('config.shipper'),
                prop: "storerkey",
                input: "input",
              },
              {
                label: this.$t('config.company'),
                prop: "company",
                input: "input",
              },
              {
                label: this.$t('config.city'),
                prop: "city",
                input: "input",
              },
              {
                label: this.$t('config.provice'),
                prop: "state",
                input: "input",
              },
              {
                label: this.$t('config.postcode'),
                prop: "zip",
                input: "input",
              },
              {
                label: this.$t('config.contact'),
                prop: "contact1",
                input: "input",
              },
              {
                label: this.$t('config.phone'),
                prop: "phone1",
                input: "input",
              },
            ],
            url: `/queryStorer/1/?/${this.$store.state.wms}`
          },
          {
            type: "input",
            label: this.$t('config.sku'),
            props: "sku",
            disabled: this.addflag ? this.addflag : this.whseidflag
          },
          {
            type: "input",
            label: this.$t('common.desc'),
            props: "descr",
            disabled: this.whseidflag
          },
          {
            type: "cascader",
            label: this.$t('common.class'),
            props: "class_",
            options:this.class_List,
            attr:{label:"typeName",value:"id",children:"node",emitPath:false},
            disabled: this.whseidflag
          },
          {
            type: "table",
            label: this.$t('config.packaging'),
            props: "packkey",
            tableConfig: [
              {
                label: this.$t('config.packaging'),
                prop: "packkey",
                input: "input",
              },
              {
                label: this.$t('common.desc'),
                prop: "packdescr",
                input: "input",
              },
            ],
            url: `/queryPack/${this.$store.state.wms}/`,
            disabled: this.whseidflag,
          },
          {
            type: "select",
            label: this.$t('outbound.lottablevalidation'),
            props: "lottablevalidationkey",
            options: this.getOptions('queryLottablevalidationDropDown'),
            disabled: this.whseidflag
          },
        ],
        [
          {
            type: "input",
            label: this.$t('config.volume'),
            props: "stdcube",
            disabled: this.whseidflag
          },
          {
            type: "input",
            label: this.$t('config.gross'),
            props: "stdgrosswgt",
            disabled: this.whseidflag
          },
          {
            type: "input",
            label: this.$t('config.suttle'),
            props: "stdnetwgt",
            disabled: this.whseidflag
          },
          {
            type: "input",
            label: this.$t('config.tareWeight'),
            props: "tare",
            disabled: this.whseidflag
          },
          {
            type: "select",
            label: this.$t('config.isQuality'),
            props: "serialnumbe",
            options: [
              { code: '0', description: this.$t('code.no') },
              { code: '1', description: this.$t('code.yes') }
            ],
            disabled: this.whseidflag
          },
          {
            type: "select",
            label: this.$t('config.serialManagement'),
            props: "scan_serial",
            options: [
              { code: '0', description: this.$t('code.no') },
              { code: '1', description: this.$t('code.yes') }
            ],
            disabled: this.whseidflag
          },
        ],
        [
          {
            type: "input",
            label: this.$t('outbound.toexpiredays'),
            props: "toexpiredays",
            disabled: this.whseidflag
          },
          {
            type: "input",
            label: this.$t('outbound.todeliverbydays'),
            props: "todeliverbydays",
            disabled: this.whseidflag
          },
          {
            type: "input",
            label: this.$t('outbound.tobestbydays'),
            props: "tobestbydays",
            disabled: this.whseidflag
          },
          {
            type: "select",
            label: this.$t('outbound.onreceiptcopypackkey'),
            props: "onreceiptcopypackkey",
            options: this.getOptions('querycodeLkup/COPYPACKTO'),
            disabled: this.whseidflag
          },
          {
            type: "checkbox",
            label: this.$t('outbound.nonstockedindicator'),
            props: "nonstockedindicator",
            disabled: this.whseidflag
          },
        ],
        [
          {
            type: "checkbox",
            label: this.$t('outbound.shelflifeindicator'),
            props: "shelflifeindicator",
            disabled: this.whseidflag
          },
          {
            type: "select",
            label: this.$t('outbound.shelflifecodetype'),
            props: "shelflifecodetype",
            options: this.getOptions('querycodeLkup/RFRCODTYPE'),
            disabled: this.whseidflag
          },
          {
            type: "input",
            label: this.$t('outbound.shelflifeonreceiving'),
            props: "shelflifeonreceiving",
            disabled: this.whseidflag || this.flag
          },
          {
            type: "input",
            label: this.$t('outbound.shelflife'),
            props: "shelflife",
            disabled: this.whseidflag || this.flag
          },
          {
            type: "input",
            label: this.$t('outbound.itemreference'),
            props: "itemreference",
            disabled: this.whseidflag
          },
        ],
        [
          {
            type: "select",
            label: "ABC",
            props: "skuabc",
            options: [
              { code: 'A', description: this.$t('code.abc1') },
              { code: 'B', description: this.$t('code.abc2') },
              { code: 'C', description: this.$t('code.abc3') },
              { code: '0', description: this.$t('code.null') }
            ],
            disabled: this.whseidflag
          },
          {
            type: "select",
            label: this.$t('outbound.readRule'),
            props: "read_rule_id",
            options: this.ruleList,
            disabled: this.whseidflag
          },
        ]
      ]
    },
  },
  methods: {
    getClassList(){
      this.$post("/baSkuType/selectBaSkuTypeList",{}).then(res => {
        this.class_List = res.data;
      })
    },
    verify (rule, value, callback) {
      if (value === '') {
        this.$message.error(this.$t('app.dataIsNotNull'))
        callback(new Error());
      } else {
        var re = /[^0-9.]/;
        let te = value;
        let flag = re.test(te)
        if (!flag) {
          if (value < 0) {
            this.$message.error(this.$t('app.inputNumberPlease'))
            this.inputData.detailData[rule.field] = 0
            callback(new Error());
          } else {
            callback();
          }
        } else {
          this.$message.error(this.$t('app.inputNumberPlease'))
          this.inputData.detailData[rule.field] = 0
          callback(new Error());
        }
      }
    },
    verify1 (rule, value, callback) {
      if (value === '') {
        callback(new Error());
      } else {
        var re = /^[0-9]*$/;
        let te = value;
        let flag = re.test(te)
        if (flag) {
          if (value < 0) {
            this.$message.error(this.$t('app.inputGreaterZeroNumberPlease'))
            this.inputData.detailData[rule.field] = 0
            callback(new Error());
          } else {
            callback();
          }
        } else {
          this.$message.error(this.$t('app.inputGreaterZeroNumberPlease'))
          this.inputData.detailData[rule.field] = 0
          callback(new Error());
        }
      }
    },
    storerkey (rule, value, callback) {
      if (value === '') {
        callback(new Error());
      } else {
        this.$api.get(`/isExists/${1}/${value}/${this.$store.state.wms}`).then(res => {
          
            if (parseInt(res.bean) <= 0) {
              // this.$message({
              //   message: `不存在的货主:${value}`,
              //   showClose: true,
              //   type: 'error'
              // })
              this.inputData.detailData.storerkey = "";
              callback(new Error());
            } else {
              callback();
            }
          
        }).catch(e => {
          this.$message({
            message: e,
            showClose: true,
            type: 'error'
          })
          callback(new Error());
        })
      }
    },
    setData (data, id, close, listId) {
      if (id == 0) {
        this.inputData.detailData.storerkey = data.storerkey;
      } else if (id == 4) {
        this.inputData.detailData.packkey = data.packkey;
      }
      close(false);
    },
    changeData(data, prop, value,nodeData){
      if(prop == 'read_rule_id'){
        this.inputData.detailData.read_rule_id = data.read_rule_id;
        this.inputData.detailData.read_rule_name = this.ruleList.find(item => item.code == data.read_rule_id)?.description;
      }else if(prop == 'class_'){
        this.inputData.detailData.class_name = nodeData[0].data.typeName
      }
    },
    getRuleList(){
      this.$post('/baBarcodeReadRule/queryBaBarcodeReadRuleList',{}).then(res => {
        this.ruleList = res.data.map(item => {const {id:code,ruleName:description} = item;return {code:String(code),description}})
      })
    }
  },
  mounted () {
    this.getRuleList()
    this.getClassList()
    if (Object.keys(this.$store.state.skip).length !== 0) {
      let data = this.$store.state.skip;
      if (data.newFlag) {
        this.addflag = false;
      } else {
        this.addflag = true;
      }
    } else {
      if (this.$store.state.newFlag) {
        this.addflag = false;
      } else {
        this.addflag = true;
      }
    }
    if (this.$store.state.wms.toUpperCase().includes('WMWHSE')) {
      //仓库级
      this.whseidflag = true;
    }
  },
  watch: {
    "inputData.detailData.shelflifeindicator": {
      handler (data) {
        if (data == 0) {
          this.flag = true;
        } else {
          this.flag = false;
        }
      },
      immediate: true,
      deep: true
    },
    "inputData.detailData":{
      handler (data) {
        if(this.inputData?.detailData?.class_){
          this.inputData.detailData.class_ = parseInt(this.inputData.detailData.class_)
        }
      },
      immediate: true,
      deep: true
    }
  },
  components: {
    "v-detail": detail
  }
}
</script>

<style scoped>
</style>
